<template>
  <div>
    <div>
      <FormPanel ref="formPanel" :list="formList" :cols="3" :labelWidth="110" :formType="type" :isSubmitBtn="false" @formChange="formChangeHandle" />
    </div>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <el-button @click="closeDrawer">取 消</el-button>
      <el-button type="primary" @click="saveDrawer">保存</el-button>
    </div>
  </div>
</template>
<script>
import { dictionary } from '@/utils/dictMixin';
import { addRescueInfo } from '@/api/customerRepair/rtmalarm/rtmalarminfo';
import { IDNumber, phone, numberCheck, email } from '@/utils/validate';
import moment from 'moment';
export default {
  mixins: [dictionary],
  props: {
    resData: {
      type: Object,
      default: {}
    }
  },
  data() {
    this.saveData = {};
    return {
      formList: this.createFormList(),
      flag: false,
      type: null
    };
  },
  created() {},
  mounted() {
    this.getData();
  },
  methods: {
    createFormList() {
      return [
        {
          type: 'INPUT',
          label: '车型',
          fieldName: 'model',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '底盘号',
          fieldName: 'vin',
          disabled: true
        },
        {
          type: 'CHECKBOX',
          label: '是否道路救援',
          fieldName: 'isRescue',
          style: { minWidth: '50px' },
          options: {
            trueValue: 10041001,
            falseValue: 10041002
          }
        },
        {
          type: 'DATE_TIME',
          label: '出发时间',
          fieldName: 'dStart',
          rules: [{ required: true, message: '请填写出发时间', trigger: 'blur',validator:this.checkDate }]
        },
        {
          type: 'DATE_TIME',
          label: '到达时间',
          fieldName: 'dReach',
          rules: [{ required: true, message: '请填写到达时间', trigger: 'blur',validator:this.checkDate }]
        },
        {
          type: 'DATE_TIME',
          label: '回店时间',
          fieldName: 'dReturn',
          rules: [{ required: true, message: '请填写回店时间', trigger: 'blur',validator:this.checkDate }]
        },
        {
          type: 'INPUT',
          label: '救援地点',
          fieldName: 'rescueAddress',
          rules: [{ required: true, message: '请填写救援地点', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '救援距离',
          fieldName: 'rescueDistance',
          rules: [{ required: true, validator: numberCheck, trigger: 'blur', message: '请输入正确格式救援距离' }]
        },
        {
          type: 'INPUT',
          label: '救援人员',
          fieldName: 'rescuePreson',
          rules: [{ required: true, message: '请填写救援人员', trigger: 'blur' }]
        },
        {
          type: 'TEXT_AREA',
          label: '过程简述',
          fieldName: 'processDescribe',
          maxlength: 500,
          rules: [{ required: true, message: '请填写过程简述', trigger: 'blur' }]
        }
      ];
    },
    formChangeHandle(val) {
      this.flag = true;
      this.saveData = val;
    },
    async getData() {
      this.formList.find(x => x.fieldName === 'model').initialValue = this.resData.MODEL;
      this.formList.find(x => x.fieldName === 'vin').initialValue = this.resData.VIN;
    },
    closeDrawer(param) {
      this.$emit('close', false);
    },
    // 校验手机
    checkDate(rule, value, callback) {
      if (!value) {
        return callback(new Error(rule.message));
      } else {
        const beginDate = this.saveData.dStart;
        const endDate = this.saveData.dReach;
        const returnDate = this.saveData.dReturn;

        console.log(beginDate);
        console.log(endDate);
        console.log(returnDate);

        // const reg = /^0\.\d+$|^[1-9]+(\.\d+)?$/;
        // if (reg.test(value)) {
        //   callback();
        // } else {
        //   return callback(new Error(rule.message));
        // }
      }
      callback();
    },
    async saveDrawer() {
      this.$refs.formPanel.SUBMIT_FORM();
      if (this.flag) {
        let obj = {
          id: this.resData.ID
        };
        this.saveData = Object.assign(this.saveData, obj);
        const beginDate = this.saveData.dStart;
        const endDate = this.saveData.dReach;
        const returnDate = this.saveData.dReturn;
        if(moment(endDate).isBefore(beginDate)){
          this.$notify.warning({ title: '提示信息', message: '出发时间不能晚于到达时间' });
          return ;
        }
        if(moment(returnDate).isBefore(endDate)){
          this.$notify.warning({ title: '提示信息', message: '到达时间不能晚于回店时间' });
          return;
        }
        if(moment(returnDate).isBefore(beginDate)){
          this.$notify.warning({ title: '提示信息', message: '出发时间不能晚于回店时间' });
          return;
        }
        const res = await addRescueInfo(this.saveData);
        console.log(res);
        if (res.resultCode == 200) {
          this.$notify.success({ title: '提示信息', message: '保存成功' });
          this.$emit('close', false);
        } else {
          this.$notify.warning({ title: '提示信息', message: '有校验信息未通过' });
        }
      }
    }
  }
};
</script>
